<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-23 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		//require_once ROOT_PATH.'include/logic/movie.logic.php';
		
		$this->excute();
	}
	
	function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			case 'trash':
				$this->trash();
				break;
			case 'do_recover':
				$this->doRecover();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		$page = $this->page_num + 1;
		
		$type = isset($this->request['type']) ? $this->request['type'] : '';
		
		$movie_id = isset($this->request['movie_id']) ? (int)$this->request['movie_id'] : '';
		$movie_name = isset($this->request['movie_name']) ? trim($this->request['movie_name']) : '';
		$title = isset($this->request['title']) ? trim($this->request['title']) : '';
		$category_id = isset($this->request['category_id']) ? (int)$this->request['category_id'] : '';
		
		$recommend1_count = isset($this->request['recommend1']) ? (int)$this->request['recommend1'] : '';
		$reply_count = isset($this->request['reply']) ? (int)$this->request['reply'] : '';
		$from_time = isset($this->request['from_time']) ? trim($this->request['from_time']) : '';
		$to_time = isset($this->request['to_time']) ? trim($this->request['to_time']) : '';
		
		//$query_link = "admin.php?m=review&page={$page}&count={$this->page_limit}";
		$query_link = "admin.php?m=review";
		$where_list = array();
		
		if($movie_id)
		{
			$where_list['mid'] = "m.id = '$movie_id'";
			$query_link .= "&movie_id=".urlencode($movie_id);
		}
		if($movie_name)
		{
			$where_list['name'] = "(lower(m.`name`) like lower('%$movie_name%') or lower(m.`name_en`) like lower('%$movie_name%'))";
			$query_link .= "&movie_name=".urlencode($movie_name);
		}
		if($title)
		{
			$where_list['title'] = "lower(mr.`title`) like lower('%$title%')";
			$query_link .= "&title=".urlencode($title);
		}
		if($category_id)
		{
			$where_list['category'] = "m.category_id = '$category_id'";
			$query_link .= "&category_id=".urlencode($category_id);
		}
		if($recommend1_count)
		{
			$where_list['recommend'] = "mr.recommend_count > '$recommend_count'";
			$query_link .= "&recommend=".urlencode($recommend_count);
		}
		if($reply_count)
		{
			$where_list['reply'] = "mr.reply_count > '$reply_count'";
			$query_link .= "&reply=".urlencode($reply_count);
		}
		
		if(strtotime($from_time) !== false)
		{
			$time = strtotime($from_time);
			//$where_list['from_time'] = "release_date > '$time'";
			$query_link .= "&from_time=".urlencode($from_time);
		}
		else
		{
			$from_time = date('Y-m-d', time() - 3600*24*30);
		}
		
		if(strtotime($to_time) !== false)
		{
			$time = strtotime($to_time);
			//$where_list['to_time'] = "release_date < '$time'";
			$query_link .= "&to_time=".urlencode($to_time);
		}
		else
		{
			$to_time = date('Y-m-d');
		}
		
		$where = empty($where_list) ? '' : 'and '.implode(' and ', $where_list).' ';
		
		$sql = " select count(1) as `total_record` 
				from `".TABLE_PREFIX."movie_review` mr 
				inner join `".TABLE_PREFIX."movie` m on mr.movie_id = m.id 
				where mr.is_delete = '0' {$where}";
		$total_record = DBFunction::getInstance()->getOne($sql);

		$item_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "select mr.*, m.name, m.category_id, m.release_date from `".TABLE_PREFIX."movie_review` mr 
					inner join `".TABLE_PREFIX."movie` m on mr.movie_id = m.id
					where
					mr.is_delete = '0'
					{$where}
					order by mr.`id` desc limit {$offset},{$this->page_limit} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
		}
		
		$results = get_movie_category_list();
		
		$category_list[] = array('name'=>'全部分类', 'value'=>'0');
		if(check_array($results))
		{
			foreach($results as $item)
			{
				$category_list[] = array('name'=>$item['name'], 'value'=>$item['id']);
			}
		}
		
		$category_select = select('category_id', $category_list, $category_id);
		
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '20 50 100 200 500');
		
		include template('admin/movie_review');
	}
	
	private function add()
	{
		$request = array();
		if(isset($this->request['c']))
		{
			$request = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$movie_id = isset($request['movie_id']) ? $request['movie_id'] : (isset($this->request['mid']) ? $this->request['mid'] : '');
		$author = isset($request['author']) ? $request['author'] : '';
		$title = isset($request['title']) ? $request['title'] : '';
		$star = isset($request['star']) ? $request['star'] : '';
		$short_desc = isset($request['short_desc']) ? $request['short_desc'] : '';
		$content = isset($request['content']) ? $request['content'] : '';
		
		$movie_name = '';
		if($movie_id)
		{
			$movie = get_movie_info($movie_id);
			$movie_name = isset($movie['name']) ? $movie['name'] : '';
		}
		
		$action = 'do_add';
		$action_name = '影评添加';
		
		include template('admin/movie_review_info');
	}
	
	private function doAdd()
	{
		$request['movie_id'] = isset($this->request['movie_id']) ? (int)$this->request['movie_id'] : '';
		$request['author'] = isset($this->request['author']) ? trim($this->request['author']) : '';
		$request['title'] = isset($this->request['title']) ? trim($this->request['title']) : '';
		$request['star'] = isset($this->request['star']) ? (int)$this->request['star'] : '';
		$request['content'] = isset($this->request['content']) ? trim($this->request['content']) : '';
		$request['short_desc'] = isset($this->request['content_short']) ? trim($this->request['content_short']) : '';
		if($request['short_desc'] == '')
		{
			$request['short_desc'] = cut_str(str_replace('[img]', '', $request['content']), 100);
		}
		else if(strlen($request['content']) > 100)
		{
			$request['short_desc'] = cut_str($request['short_desc'], 100);
		}
		
		//$request['content'] = str_replace("\n", '\n', $request['content']);
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "admin.php?m=review&a=add&c=$key";
		
		$movie = ($request['movie_id'] != '') ? get_movie_info($request['movie_id']) : array();
				
		$message = '';
		if(!isset($movie['id']) || !$movie['id'])
		{
			$this->messager("影片ID错误", $redirect);
		}
		if(!$request['author'] || !$request['title'] || !$request['content'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		if(strlen($request['author']) > 90)
		{
			$this->messager("作者内容超长", $redirect);
		}
		if(strlen($request['title']) > 90)
		{
			$this->messager("标题内容超长", $redirect);
		}
		
		$is_exists = check_movie_review_exists($request['title'], $request['movie_id']);
		if($is_exists)
		{
			$this->messager("影评标题《{$request['title']}》已存在", $redirect);
		}
		
		$result = add_movie_review($request);
		
		if($result)
		{
			//$this->messager("添加成功，继续添加，<a href='admin.php?m=review&a=add&id={$request['movie_id']}' style='color:red'>请点击</a>", 'admin.php?m=review&a=modify&id='.$result);
			$this->messager("添加成功", 'admin.php?m=review&a=modify&id='.$result);
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function doDelete()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		if(!$ids)
		{
			$this->messager("请指定ID", 'admin.php?m=review');
		}
		
		// delete movie review replys
		$trash_rids = get_trash_review_reply_ids($ids);
		if(check_array($trash_rids))
		{
			$rids = array();
			foreach($trash_rids as $item)
			{				
				$rids[] = $item['id'];
			}
			delete_movie_review_reply(implode(',', $rids));
		}
		
		// delete movie review description
		$trash_rids = get_trash_review_ids($ids);
		if(check_array($trash_rids))
		{
			$rids = array();
			foreach($trash_rids as $item)
			{
				$rids[] = $item['id'];
			}
			delete_movie_review_desc(implode(',', $rids));
		}
		
		$result = delete_movie_review($ids);

		if($result)
		{
			$this->messager("删除成功", 'admin.php?m=review');
		}
		else
		{
			$this->messager("删除失败", 'admin.php?m=review');
		}
	}
	
	private function modify()
	{
		$movie_review_id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$movie_review_id)
		{
			$this->messager("请指定ID", 'admin.php?m=review');
		}
		
		if(isset($this->request['c']) && ($movie_review = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$movie_review = get_movie_review_info($movie_review_id);
			
			if(!isset($movie_review['id']) || !$movie_review['id'])
			{
				$this->messager("影评不存在", 'admin.php?m=review');
			}
		}
		
		$id = isset($movie_review['id']) ? $movie_review['id'] : '';
		$movie_id = isset($movie_review['movie_id']) ? $movie_review['movie_id'] : '';
		$author = isset($movie_review['author']) ? $movie_review['author'] : '';
		$title = isset($movie_review['title']) ? $movie_review['title'] : '';
		$star = isset($movie_review['star_level']) ? $movie_review['star_level'] : '';
		$short_desc = isset($movie_review['short_desc']) ? $movie_review['short_desc'] : '';
		$content = isset($movie_review['content']) ? $movie_review['content'] : '';
		
		$images = get_movie_image_list($id, '3');
		$image_list = array();
		if(check_array($images))
		{
			foreach($images as $item)
			{
				$image_list[$item['id']]['1'] = 'data/images/movie/'.image_path($item['id']).$item['id'] . '_1.jpg';
				$image_list[$item['id']]['2'] = 'data/images/movie/'.image_path($item['id']).$item['id'] . '_2.jpg';
			}
		}
		
		$action = 'do_modify&id='.$id;
		$action_name = '影评修改';
		
		include template('admin/movie_review_info');
	}
	
	private function doModify()
	{
		$request['id'] = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$request['movie_id'] = isset($this->request['movie_id']) ? (int)$this->request['movie_id'] : '';
		$request['author'] = isset($this->request['author']) ? trim($this->request['author']) : '';
		$request['star'] = isset($this->request['star']) ? (int)$this->request['star'] : '';
		$request['title'] = isset($this->request['title']) ? trim($this->request['title']) : '';
		$request['content'] = isset($this->request['content']) ? trim($this->request['content']) : '';
		$request['short_desc'] = isset($this->request['content_short']) ? trim($this->request['content_short']) : '';
		if($request['short_desc'] == '')
		{
			$request['short_desc'] = cut_str(str_replace('[img]', '', $request['content']), 100);
		}
		else if(strlen($request['content']) > 100)
		{
			$request['short_desc'] = cut_str($request['short_desc'], 100);
		}
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "admin.php?m=review&a=modify&id={$request['id']}&c=$key";
		
		$movie_review = get_movie_review_info($request['id']);
				
		$message = '';
		if(!isset($movie_review['id']) || !$movie_review['id'])
		{
			$this->messager("影评不存在", $redirect);
		}
		if(!$request['author'] || !$request['title'] || !$request['content'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		if(strlen($request['author']) > 45)
		{
			$this->messager("作者内容超长", $redirect);
		}
		if(strlen($request['title']) > 95)
		{
			$this->messager("标题内容超长", $redirect);
		}
		
		$is_exists = check_movie_review_exists($request['title'], $request['movie_id'], $request['id']);
		if($is_exists)
		{
			$this->messager("影评标题《{$request['title']}》已存在", $redirect);
		}
		
		$result = update_movie_review($request);
		
		if($result)
		{
			$this->messager("修改成功", 'admin.php?m=review');
		}
		else
		{
			$this->messager("修改失败", $redirect);
		}
	}
	
	private function trash()
	{
		$page = $this->page_num + 1;
				
		$sql = " select count(1) as `total_record` from `".TABLE_PREFIX."movie_review` mr where mr.is_delete = '1'";
		$total_record = DBFunction::getInstance()->getOne($sql);

		$item_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "select mr.*, m.name, m.category_id, m.release_date from `".TABLE_PREFIX."movie_review` mr 
					inner join `".TABLE_PREFIX."movie` m on mr.movie_id = m.id
					where
					mr.is_delete = '1'
					order by mr.`id` desc limit {$offset},{$this->page_limit} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
		}
		
		$query_link = 'admin.php?m=review&a=trash';
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '20 50 100 200 500');
		
		include template('admin/movie_review_trash');
	}
	
	private function doRecover()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		$redirect = 'admin.php?m=review&a=trash';
		
		if(!$ids)
		{
			$this->messager("请指定ID", $redirect);
		}

		$result = recover_movie_review($ids);
		
		if($result)
		{
			$this->messager("操作成功", $redirect);
		}
		else
		{
			$this->messager("操作失败", $redirect);
		}
	}
}